/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.clinicplus.manager.search;

import br.com.foxline.clinicplus.entity.Operator;
import br.com.foxline.clinicplus.entity.Procedure;
import br.com.foxline.clinicplus.entity.ValueProcedure;
import br.com.foxline.clinicplus.enums.GroupProcedure;
import br.com.foxline.clinicplus.enums.TypeProcedure;
import br.com.foxline.clinicplus.service.OperatorService;
import br.com.foxline.clinicplus.service.ProcedureService;
import br.com.foxline.clinicplus.service.ValueProcedureService;
import br.com.foxline.clinicplus.util.Msg;
import br.com.foxline.clinicplus.vo.VoProcedureValue;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

/**
 *
 * @author ely
 */
@ManagedBean
@ViewScoped
public class ManagerSearchProcedure {

    @EJB
    ProcedureService procedureService;
    @EJB
    OperatorService operatorService;
    @EJB
    ValueProcedureService valueProcedureService;
    private Procedure editProcedure;
    private Procedure searchProcedure;
    private Procedure showProcedure;
    private List<Procedure> procedures;
    private String proEdit;
    private String proShow;
    private Operator operator;
    private ValueProcedure valueProcedure;
    private List<ValueProcedure> valueProcedures;
    private Operator searchOperator;
    private List<Procedure> lstSearchProcedure;
    //RELATÓRIO 
    private List<VoProcedureValue> voProcedureValues;
    private List<VoProcedureValue> selectVoProcedureValues;
    private ValueProcedure vp;
    private BigDecimal newValue;
    private VoProcedureValue voProcedureValue;

    @PostConstruct
    public void init() {
        searchProcedure = new Procedure();
        searchOperator = new Operator();
        Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();

        proEdit = params.get("proEdit");
        proShow = params.get("proShow");

        paramPost();

    }

    public List<SelectItem> selectTypeProcedure() {
        List<SelectItem> list = new ArrayList<SelectItem>();

        for (TypeProcedure ms : TypeProcedure.values()) {
            list.add(new SelectItem(ms, ms.getName()));
        }
        return list;
    }

    public List<SelectItem> selectGroupProcedure() {
        List<SelectItem> list = new ArrayList<SelectItem>();

        for (GroupProcedure ms : GroupProcedure.values()) {
            list.add(new SelectItem(ms, ms.getName()));
        }
        return list;
    }

    public void addValueProcedure() {
        for (ValueProcedure vp : this.valueProcedures) {
            if (vp.getOperator().getId() == operator.getId()) {
                Msg.messagemWarn("Já existe um valor declarado para esse convênio");
                return;
            }
        }

        this.valueProcedure.setOperator(operator);
        this.valueProcedures.add(valueProcedure);
        this.operator = new Operator();
        this.valueProcedure = new ValueProcedure();
    }

    public void listProcedure() {
        procedures = procedureService.findProcedure(searchProcedure, false);
    }

    
    public void pesquisarValoresProcedimento() {
        voProcedureValues = procedureService.findVoProcedureOperator(searchOperator);
        selectVoProcedureValues = voProcedureValues;
    }
    
    public void relProcedure() {
//        procedures = procedureService.findProcedureOperator(searchOperator);
        procedureService.reportProcedureOperator(searchOperator, selectVoProcedureValues);
    }

    public void paramPost() {
        try {
            if (proShow != null) {
                showProcedure = procedureService.find(Long.parseLong(proShow));

            } else if (proEdit != null) {
                this.editProcedure = procedureService.find(Long.parseLong(proEdit));
                this.operator = new Operator();
                this.valueProcedure = new ValueProcedure();

                if (!this.editProcedure.getValueProcedure().isEmpty()) {
                    this.valueProcedures = this.editProcedure.getValueProcedure();
                } else {
                    this.valueProcedures = new ArrayList<ValueProcedure>();
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void removeValue(int i) {
        this.valueProcedures.remove(i);
    }

    public void editValue(int i) {
        this.valueProcedure = this.valueProcedures.get(i);
        this.operator = this.valueProcedure.getOperator();
        removeValue(i);
    }

    public void update() {
        try {
            editProcedure.setValueProcedure(valueProcedures);
            procedureService.update(editProcedure);
            Msg.messagemInfo("Procedimento Atualizado com Sucesso ");
        } catch (Exception erro) {
            Msg.messagemInfo("Erro ao Atualizar Procedimento");
        }
    }

    public List<Operator> autocomplete(String query) {
        return operatorService.autocomplete(query);
    }

    public Procedure getEditProcedure() {
        return editProcedure;
    }

    public void setEditProcedure(Procedure editProcedure) {
        this.editProcedure = editProcedure;
    }

    public Procedure getSearchProcedure() {
        return searchProcedure;
    }

    public void setSearchProcedure(Procedure searchProcedure) {
        this.searchProcedure = searchProcedure;
    }

    public Procedure getShowProcedure() {
        return showProcedure;
    }

    public void setShowProcedure(Procedure showProcedure) {
        this.showProcedure = showProcedure;
    }

    public List<Procedure> getProcedures() {
        return procedures;
    }

    public void setProcedures(List<Procedure> procedures) {
        this.procedures = procedures;
    }

    public Operator getOperator() {
        return operator;
    }

    public void setOperator(Operator operator) {
        this.operator = operator;
    }

    public ValueProcedure getValueProcedure() {
        return valueProcedure;
    }

    public void setValueProcedure(ValueProcedure valueProcedure) {
        this.valueProcedure = valueProcedure;
    }

    public List<ValueProcedure> getValueProcedures() {
        return valueProcedures;
    }

    public void setValueProcedures(List<ValueProcedure> valueProcedures) {
        this.valueProcedures = valueProcedures;
    }

    public Operator getSearchOperator() {
        return searchOperator;
    }

    public void setSearchOperator(Operator searchOperator) {
        this.searchOperator = searchOperator;
    }

    public List<Procedure> getLstSearchProcedure() {
        return lstSearchProcedure;
    }

    public void setLstSearchProcedure(List<Procedure> lstSearchProcedure) {
        this.lstSearchProcedure = lstSearchProcedure;
    }

    public List<VoProcedureValue> getSelectVoProcedureValues() {
        return selectVoProcedureValues;
    }

    public void setSelectVoProcedureValues(List<VoProcedureValue> selectVoProcedureValues) {
        this.selectVoProcedureValues = selectVoProcedureValues;
    }

    public List<VoProcedureValue> getVoProcedureValues() {
        return voProcedureValues;
    }

    public void setVoProcedureValues(List<VoProcedureValue> voProcedureValues) {
        this.voProcedureValues = voProcedureValues;
    }

    public void loadValue(int id) {
        voProcedureValue = voProcedureValues.get(id);
        vp = valueProcedureService.find(voProcedureValue.getId());
        newValue = vp.getAmount();
    }

    public void alterValueProcedure() {
        try {
            vp.setAmount(newValue);
            voProcedureValue.setAmount(newValue);
            valueProcedureService.update(vp);
            Msg.messagemInfo();
        } catch (Exception e) {
            Msg.messagemError();
        }
    }

    public BigDecimal getNewValue() {
        return newValue;
    }

    public void setNewValue(BigDecimal newValue) {
        this.newValue = newValue;
    }
}
